package com.example;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

@SpringBootApplication
@EnableCaching
@EnableTransactionManagement // 启注解事务管理，等同于xml配置方式的 <tx:annotation-driven />
public class SpringDataJpaExampleApplication {

//	@Resource(name="transactionManager")
//    private PlatformTransactionManager transactionManager;
//
//    // 创建事务管理器1
//    @Bean(name = "transactionManager")
//    public PlatformTransactionManager txManager(DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }
//
//    // 创建事务管理器2
//    @Bean(name = "txManager2")
//    public PlatformTransactionManager txManager2(EntityManagerFactory factory) {
//        return new JpaTransactionManager(factory);
//    }
//
//    // 实现接口 TransactionManagementConfigurer 方法，其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
//    @Override
//    public PlatformTransactionManager annotationDrivenTransactionManager() {
//        return transactionManager;
//    }
//	
	public static void main(String[] args) {
		SpringApplication.run(SpringDataJpaExampleApplication.class, args);
	}
	
	// http请求定向至https
//	 @Bean
//	    public EmbeddedServletContainerFactory servletContainer() {
//	        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
//	            @Override
//	            protected void postProcessContext(Context context) {
//	                SecurityConstraint constraint = new SecurityConstraint();
//	                constraint.setUserConstraint("CONFIDENTIAL");
//	                SecurityCollection collection = new SecurityCollection();
//	                collection.addPattern("/*");
//	                constraint.addCollection(collection);
//	                context.addConstraint(constraint);
//	            }
//	        };
//	        tomcat.addAdditionalTomcatConnectors(httpConnector());
//	        return tomcat;
//	    }
//
//	    @Bean
//	    public Connector httpConnector() {
//	        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//	        connector.setScheme("http");
//	        //Connector监听的http的端口号
//	        connector.setPort(8080);
//	        connector.setSecure(false);
//	        //监听到http的端口号后转向到的https的端口号
//	        connector.setRedirectPort(8443);
//	        return connector;
//	    }
}
